package net.zjitc.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {
    private Log log=LogFactory.getLog(this.getClass());
    /*登录界面
    * */
//    private String message;

    private User model=new User();
    @Resource
    private UserService userService;

//    public String getMessage() {
//        return message;
//    }
//
//    public void setMessage(String message) {
//        this.message = message;
//    }

//    public String getUsername() {
//        return username;
//    }
//
//    public void setUsername(String username) {
//        this.username = username;
//    }
//
//    public String getPassword() {
//        return password;
//    }
//
//    public void setPassword(String password) {
//        this.password = password;
//    }

    public String loginUI() {
        return "loginUI";
    }

    //    登录
    public String login() {
//     如果有用户的话。返回登录成功，否则返回失败
        User user = userService.findByUsernamePassword(model.getUsername(),model.getPassword());

//        ActionContext.getContext().getValueStack().push(user);
        ActionContext.getContext().getSession().put("user",user);
        System.out.println("name="+model.getUsername());
        System.out.println("psw="+model.getPassword());
        if (user != null) {
//            message = "登陆成功";
            log.debug("登陆成功");
         this.addActionMessage("登陆成功");
            List<User>userList=userService.findAll();
//            将查找出来的对象放到值占空间

            ActionContext.getContext().put("userList",userList);
            return "loginSuccess";
        }
//        message = "用户名或密码错误";
        this.addActionError("用户名或密码错误");
        return "loginFail";
    }


    @Override
    public User getModel() {
        return model;
    }
}
